﻿using System;
using System.Collections.Generic;
using NHibernate;
using Common;
using Educational.Model.Entity;
using Educational.Model.EntityManager;

namespace Educational.Business
{
    public class TeacherAdmin
    {
        #region DeleteTeacher删除一组教师
        public static void DeleteTeacher(int schoolId, int[] teacherIds)
        {
            ISession session = null;
            ITransaction trans = null;
            try
            {
                session = NHibernateHelper.OpenSession();
                trans = session.BeginTransaction();
                IList<Basic.Model.Entity.Collision> collisions = new List<Basic.Model.Entity.Collision>();
                for (int i = 0; i < teacherIds.Length; i++)
                {
                    Basic.Model.Entity.Collision collision = new Basic.Model.Entity.Collision();
                    collision.itemKey = "DeleteTeacher" + teacherIds[i].ToString();
                    Basic.Model.EntityManager.CollisionManager.AddItem(session, collision);
                    collisions.Add(collision);
                }
                IList<Teacher> list = TeacherManager.Gets2(session, schoolId, teacherIds);
                for (int i = 0; i < list.Count; i++)
                {
                    list[i].status = 8;
                }
                for (int i = 0; i < collisions.Count; i++)
                {
                    Basic.Model.EntityManager.CollisionManager.DeleteItem(session, collisions[i]);
                }
                trans.Commit();
            }
            catch
            {
                if (trans != null)
                    trans.Rollback();
                throw;
            }
            finally
            {
                if (session != null)
                    session.Close();
            }
        }
        #endregion

        #region UpdateTeacher修改一个教师
        /// <summary>
        /// 修改本校教师信息
        /// </summary>
        /// <param name="teacherId"></param>
        /// <param name="loginMd5Pass"></param>
        /// <param name="jobTitleId"></param>
        /// <param name="schoolId"></param>
        public static void UpdateTeacher(
            int teacherId,
            string loginMd5Pass,
            int status,
            int jobTitleId,
            int schoolId
        )
        {
            ISession session = null;
            ITransaction trans = null;
            Teacher teacher = null;
            try
            {
                session = NHibernateHelper.OpenSession();
                trans = session.BeginTransaction();

                teacher = TeacherManager.Get(session, teacherId);
                if (teacher == null)
                    throw new Exception("没有这个教师或者这个教师并不存在！");
                if (teacher.school.schoolId != schoolId)
                    throw new Exception("你在执行一个你没有权限的操作。经过系统判断，你应该在试探攻击本系统，送你两个字：SB");
                teacher.jobTitle = Basic.Model.EntityManager.DictionaryManager.Get(session, jobTitleId);
                if (teacher.jobTitle == null)
                    throw new Exception("当前字典不存在这个值");
                if (!String.IsNullOrEmpty(loginMd5Pass))
                    teacher.loginPass = loginMd5Pass;
                if (status > 0)
                    teacher.status = status;
                trans.Commit();
            }
            catch
            {
                if (trans != null)
                    trans.Rollback();
                throw;
            }
            finally
            {
                if (session != null)
                    session.Close();
            }
        }
        #endregion

        #region AddTeacher添加一个教师
        /// <summary>
        /// AddTeacher添加一个教师
        /// </summary>
        /// <param name="teacherName"></param>
        /// <param name="loginId"></param>
        /// <param name="loginMd5Pass"></param>
        /// <param name="jobTitleId"></param>
        /// <param name="schoolId"></param>
        public static void AddTeacher(
            string teacherName,
            string loginId,
            string loginMd5Pass,
            int jobTitleId,
            int schoolId
        )
        {
            if (String.IsNullOrEmpty(teacherName) || String.IsNullOrEmpty(loginId))
                throw new Exception("信息填写不完整，教师姓名，登录Id不能为空！");
            ISession session = null;
            ITransaction trans = null;
            Teacher teacher = null;
            try
            {
                session = NHibernateHelper.OpenSession();
                trans = session.BeginTransaction();

                if (TeacherManager.Get1(session, loginId) != null)
                    throw new Exception("当前系统已存在这个登录ID，请换一个登录ID。");
                teacher = new Teacher();
                teacher.teacherName = teacherName;
                teacher.teacherSex = "未知";
                teacher.phoneNumber = "";
                teacher.lastLoginIp = "";
                teacher.lastLoginTime = DateTime.Now;
                teacher.loginId = loginId;
                teacher.loginPass = loginMd5Pass;
                teacher.loginTimes = 0;
                teacher.regTime = DateTime.Now;
                teacher.status = 9;
                teacher.jobTitle = Basic.Model.EntityManager.DictionaryManager.Get(session, jobTitleId);
                if (teacher.jobTitle == null)
                    throw new Exception("当前字典不存在这个值");
                teacher.school = SchoolManager.Get(session, schoolId);
                if (teacher.school == null)
                    throw new Exception("当前学校不存在");
                TeacherManager.Add(session, teacher);

                trans.Commit();
            }
            catch
            {
                if (trans != null)
                    trans.Rollback();
                throw;
            }
            finally
            {
                if (session != null)
                    session.Close();
            }
        }
        #endregion

        #region GetsTeacher查询所有教师根据分页
        /// <summary>
        /// 查询所有教师根据分页
        /// </summary>
        /// <param name="teacherName">教师名称，非必填</param>
        /// <param name="loginId">登录ID，非必填</param>
        /// <param name="schoolId">学校Id，非必填</param>
        /// <param name="rowIndex"></param>
        /// <param name="rowCount"></param>
        /// <returns></returns>
        public static PageResult<Educational.VO.Teacher> GetsTeacher(string teacherName, string loginId, int schoolId, int rowIndex, int rowCount)
        {
            IList<Teacher> entityData = null;
            PageResult<Educational.VO.Teacher> pageResult = new PageResult<Educational.VO.Teacher>();
            pageResult.data = new List<Educational.VO.Teacher>();
            ISession session = null;
            ITransaction trans = null;
            try
            {
                session = NHibernateHelper.OpenSession();
                trans = session.BeginTransaction();
                entityData = TeacherManager.Gets1(session, teacherName, loginId, schoolId, rowIndex, rowCount);
                pageResult.totalCount = TeacherManager.Gets1Count(session, teacherName, loginId, schoolId);
                trans.Commit();
            }
            catch
            {
                if (trans != null)
                    trans.Rollback();
                throw;
            }
            finally
            {
                if (session != null)
                    session.Close();
            }
            for (int i = 0; i < entityData.Count; i++)
            {
                Educational.VO.Teacher teacher = new Educational.VO.Teacher();
                teacher.lastLoginIp = entityData[i].lastLoginIp;
                teacher.lastLoginTime = Tools.ToDateTimeString(entityData[i].lastLoginTime);
                teacher.loginId = entityData[i].loginId;
                teacher.loginTimes = entityData[i].loginTimes;
                teacher.regTime = Tools.ToDateTimeString(entityData[i].regTime);
                teacher.teacherId = entityData[i].teacherId;
                teacher.teacherName = entityData[i].teacherName;
                teacher.teacherSex = entityData[i].teacherSex;
                teacher.phoneNumber = entityData[i].phoneNumber;
                teacher.status = entityData[i].status;
                teacher.jobTitle = new Basic.VO.Dictionary();
                teacher.jobTitle.dictionaryId = entityData[i].jobTitle.dictionaryId;
                teacher.jobTitle.dictionaryValue = entityData[i].jobTitle.dictionaryValue;
                pageResult.data.Add(teacher);
            }
            return pageResult;
        }
        #endregion

        #region GetsTeacherForSelect查询状态正常教师根据分页
        /// <summary>
        /// 查询状态正常教师根据分页
        /// </summary>
        /// <param name="teacherName">教师名称，非必填</param>
        /// <param name="loginId">登录ID，非必填</param>
        /// <param name="schoolId">学校Id，非必填</param>
        /// <param name="rowIndex"></param>
        /// <param name="rowCount"></param>
        /// <returns></returns>
        public static PageResult<Educational.VO.Teacher> GetsTeacherForSelect(string teacherName, string loginId, int schoolId, int rowIndex, int rowCount)
        {
            IList<Teacher> entityData = null;
            PageResult<Educational.VO.Teacher> pageResult = new PageResult<Educational.VO.Teacher>();
            pageResult.data = new List<Educational.VO.Teacher>();
            ISession session = null;
            ITransaction trans = null;
            try
            {
                session = NHibernateHelper.OpenSession();
                trans = session.BeginTransaction();
                entityData = TeacherManager.Gets3(session, teacherName, loginId, schoolId, rowIndex, rowCount);
                pageResult.totalCount = TeacherManager.Gets3Count(session, teacherName, loginId, schoolId);
                trans.Commit();
            }
            catch
            {
                if (trans != null)
                    trans.Rollback();
                throw;
            }
            finally
            {
                if (session != null)
                    session.Close();
            }
            for (int i = 0; i < entityData.Count; i++)
            {
                Educational.VO.Teacher teacher = new Educational.VO.Teacher();
                teacher.loginId = entityData[i].loginId;
                teacher.regTime = Tools.ToDateTimeString(entityData[i].regTime);
                teacher.teacherId = entityData[i].teacherId;
                teacher.teacherName = entityData[i].teacherName;
                teacher.teacherSex = entityData[i].teacherSex;
                teacher.phoneNumber = entityData[i].phoneNumber;
                teacher.status = entityData[i].status;
                teacher.jobTitle = new Basic.VO.Dictionary();
                teacher.jobTitle.dictionaryId = entityData[i].jobTitle.dictionaryId;
                teacher.jobTitle.dictionaryValue = entityData[i].jobTitle.dictionaryValue;
                pageResult.data.Add(teacher);
            }
            return pageResult;
        }
        #endregion
    }
}
